package org.japo.java.matisse.samples;

import java.awt.event.ActionEvent;

public class ConvertirDolaEuroAsistido extends javax.swing.JFrame {

    // Factor de conversión E > D
    private double factor = 1.32;

    public ConvertirDolaEuroAsistido() {
        // Antes de la construcción del interfaz
        iniciarAnterior();

        // Construcción del interfaz
        initComponents();

        // Después de la construcción del interfaz
        iniciarPosterior();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblEuro = new javax.swing.JLabel();
        txfEuro = new javax.swing.JTextField();
        lblDolar = new javax.swing.JLabel();
        txfDolar = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Conversor de moneda");
        setResizable(false);

        lblEuro.setFont(new java.awt.Font("Calibri", 1, 48)); // NOI18N
        lblEuro.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblEuro.setText("Euros");

        txfEuro.setFont(new java.awt.Font("Calibri", 1, 48)); // NOI18N
        txfEuro.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txfEuro.setText("0.0");
        txfEuro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfEuroActionPerformed(evt);
            }
        });

        lblDolar.setFont(new java.awt.Font("Calibri", 1, 48)); // NOI18N
        lblDolar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblDolar.setText("Dólares");

        txfDolar.setFont(new java.awt.Font("Calibri", 1, 48)); // NOI18N
        txfDolar.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txfDolar.setText("0.0");
        txfDolar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfDolarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(txfEuro, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblEuro, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(txfDolar, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblDolar, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txfEuro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblEuro))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txfDolar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDolar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txfEuroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfEuroActionPerformed
        procesarConversion(evt);
    }//GEN-LAST:event_txfEuroActionPerformed

    private void txfDolarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfDolarActionPerformed
        procesarConversion(evt);
    }//GEN-LAST:event_txfDolarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConvertirDolaEuroAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConvertirDolaEuroAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConvertirDolaEuroAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConvertirDolaEuroAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ConvertirDolaEuroAsistido().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblDolar;
    private javax.swing.JLabel lblEuro;
    private javax.swing.JTextField txfDolar;
    private javax.swing.JTextField txfEuro;
    // End of variables declaration//GEN-END:variables

    private void iniciarAnterior() {
    }

    private void iniciarPosterior() {
    }

    public double getFactor() {
        return factor;
    }

    public void setFactor(double factor) {
        this.factor = factor;
    }

    private void procesarConversion(ActionEvent ae) {
        // Referencia al control que ha generado el evento
        Object o = ae.getSource();

        // Discriminar la conversión
        if (o.equals(txfEuro)) {
            convertirEuros();
        } else {
            convertirDolares();
        }
    }

    private void convertirEuros() {
        try {
            String textoEuros = txfEuro.getText();
            double dineroEuros = Double.parseDouble(textoEuros);
            double dineroDolar = dineroEuros * getFactor();
            String textoDolar = String.format("%.2f", dineroDolar);
            txfDolar.setText(textoDolar);
        } catch (Exception e) {
            txfDolar.setText("???");
        }
    }

    private void convertirDolares() {
        try {
            String textoDolar = txfDolar.getText();
            double dineroDolar = Double.parseDouble(textoDolar);
            double dineroEuro = dineroDolar / getFactor();
            String textoEuro = String.format("%.2f", dineroEuro);
            txfEuro.setText(textoEuro);
        } catch (Exception e) {
            txfEuro.setText("???");
        }
    }
}
